package com.terrapages.wmsc4j.bean;


/**
 * BBox (AKA: Bounds, Spatial Bounding Box) is a spatial area of a box formed between 2 coordinate positions.
 * @author mvivian
 */		
public class BBox{

	private static final long serialVersionUID = 1L;
	
	private Pos minPos;
	private Pos maxPos;
	
	/**
	 * Creates a BBox from (minX, minY) to (maxX,maxY).
	 * 
	 * @param minX The smallest longitude coordinate of the BBox
	 * @param minY The smallest latitude  coordinate of the BBox
	 * @param maxX The largest longitude coordinate of the BBox
	 * @param maxY The largest latitude  coordinate of the BBox
	 */
	public BBox(double minX, double minY,double maxX, double maxY) {
		minPos = new Pos(minX,minY);
		maxPos = new Pos(maxX,maxY);
	}
	
	/**
	 * Creates a BBox from minPos to maxPos.
	 * 
	 * @param minPos The minimum coordinate {@link Pos} of the BBox. I.E. The most South-Western point of the BBox
	 * @param maxPos The maximum coordinate {@link Pos} of the BBox. I.E. The most North-Eastern point of the BBox
	 * @throws IllegalArgumentException If the attributes of first argument Pos are greater than the coresponding attributes of the second argument Pos
	 */
	public BBox(Pos minPos, Pos maxPos) {
		super();
		if(minPos.getLatitude() > maxPos.getLatitude() || minPos.getLongitude() > maxPos.getLongitude())
		{
			throw new IllegalArgumentException("Attributes of first argument Pos must be less than the coresponding attributes of the second argument Pos");
		}
		this.minPos = minPos;
		this.maxPos = maxPos;
	}

	/**
	 * Gets the maximum coordinate {@link Pos} of the BBox 
	 * I.E. The most North-Eastern point of the BBox.
	 * @return Pos 
	 */
	public Pos getMaxPos() {
		return maxPos;
	}

	/**
	 * Sets the maximum coordinate {@link Pos} of the BBox.
	 * I.E. The most North-Eastern point of the BBox.
	 * @param maxPos
	 */
	public void setMaxPos(final Pos maxPos) {
		this.maxPos = maxPos;
	}

	/**
	 * Gets the minimum coordinate {@link Pos} of the BBox. 
	 * I.E. The most South-Western point of the BBox.
	 * @return Pos 
	 */
	public Pos getMinPos() {
		return minPos;
	}

	/**
	 * Gets the minimum coordinate {@link Pos} of the BBox .
	 * I.E. The most South-Western point of the BBox
	 * @param minPos
	 */
	public void setMinPos(final Pos minPos) {
		this.minPos = minPos;
	}

	/**
	 * Gets the western most longitude of the BBox
	 * @return double
	 */
	public double getMinLongitude()
	{
		return minPos.getLongitude();
	}
	
	/**
	 * Gets the northern most latitude of the BBox
	 * @return double
	 */
	public double getMinLatitude()
	{
		return minPos.getLatitude();
	}
	
	/**
	 * Gets the eastern most longitude of the BBox
	 * @return double
	 */
	public double getMaxLongitude()
	{
		return maxPos.getLongitude();
	}
	
	/**
	 * Gets the southern most latitude of the BBox
	 * @return double
	 */
	public double getMaxLatitude()
	{
		return maxPos.getLatitude();
	}
	
	/**
	 * Gets the longitudinal width of theBox in degrees. 
	 * I.E. the difference between the western most and eastern most longitude.
	 * @return double
	 */
	public double getWidth()
	{
		return maxPos.getLongitude() - minPos.getLongitude();
	}
	
	/**
	 * Gets the latitudinal height of theBox in degrees. 
	 * I.E. the difference between the northern most and southern most latitude.
	 * @return double
	 */
	public double getHeight()
	{
		return maxPos.getLatitude() - minPos.getLatitude();
	}
	
	/**
	 * Gets the {@link Pos} of the centre of the BBox.
	 * I.E. half way between minPos and maxPos
	 * @return
	 */
	public Pos getCentrePos()
	{
		return new Pos(
			(maxPos.getLongitude() + minPos.getLongitude())/2,
			(maxPos.getLatitude() + minPos.getLatitude())/2
		);
	}
}
